﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyOA.Model;
using System.Data;
using System.Data.SqlClient;

namespace MyOA.DAL
{
    public class FileSearchService
    {


        //根据输入条件查询文件信息
        public IList<FileInfo> GetFilesInfoByConditions(string fileName, string createUser, string containWods, string beginDate, string endDate)
        {
            string sqlFileName = "";
            if (fileName != "")
            {
                sqlFileName = " and a.filesname like '%" + fileName + "%'";
            }

            string sqlTime = "";
            if (beginDate != "" && endDate != "")
            {
                DateTime beginTime = DateTime.Parse(beginDate);
                DateTime endTime = DateTime.Parse(endDate);
                sqlTime = " and a.createdate between Convert(DateTime,'" + beginTime + "') and Convert(DateTime,'" + endTime + "') ";
            }

            string sql = "SELECT a.* FROM FileInfo a where ifdelete=0 ";


            if (createUser != "")
            {
                sql = "SELECT a.* FROM FileInfo a,userinfo b where a. ifdelete=0 and a.fileownerid = b.userid ";

                sql += " and b.username like '%" + createUser + "%'";

            }
            sql += sqlFileName;
            sql += sqlTime;

            return GetFileInfosBySql(sql);
        }
        DBHelper db = new DBHelper();
        RecycleBinService rbs = new RecycleBinService();
        public IList<FileInfo> GetFileInfosBySql(string safeSql)
        {
            List<FileInfo> list = new List<FileInfo>();
            int FileTypeId = 0;
            int FileOwnerId = 0;
            try
            {
                DataTable table = db.GetDataSet1(safeSql);

                foreach (DataRow row in table.Rows)
                {
                    FileInfo fileInfo = new FileInfo();


                    fileInfo.FileId = (int)row["FileId"];
                    fileInfo.FilesName = (string)row["FilesName"];
                    FileTypeId = (int)row["FileTypeid"];
                    fileInfo.FileType = rbs.GetFileTypeById(FileTypeId);
                    FileOwnerId = (int)row["FileOwnerid"];
                    fileInfo.FileOwner = rbs.GetUserInfoById(FileOwnerId);
                    fileInfo.Remark = (string)row["Remark"];

                    fileInfo.CreateDate = (DateTime)row["CreateDate"];
                    fileInfo.ParentId = (int)row["ParentId"];
                    fileInfo.FilePath = (string)row["FilePath"];
                    fileInfo.IfDelete = (int)row["IfDelete"];

                    list.Add(fileInfo);
                }

                return list;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }

        }
    }
}
